<template>
  <div>
    <!-- 基本信息 -->
    <div class="newAccountReport-content">
      <div class="newAccountReport-content-title">基本信息</div>
      <div class="newAccountReport-content-form">
        <normal-form-plus
          ref="myForm1"
          :formModel="formModels.formModel1"
          :config="configs.config1"
          :plus="plus"/>
      </div>
    </div>

    <!-- 报告负责人信息 -->
    <div class="newAccountReport-content">
      <div class="newAccountReport-content-title">报告负责人信息</div>
      <div class="newAccountReport-content-form">
        <normal-form-plus
          :formModel="formModels.formModel2"
          :config="configs.config2"
          :plus="plus"/>
      </div>
    </div>

    <!-- 减排信息 -->
    <div class="newAccountReport-content">
      <div class="newAccountReport-content-title">减排信息</div>
      <div class="newAccountReport-content-form">
        <div class="newAccountReport-content-form-btn">
          <el-button @click="turn">选择减排场景</el-button>
        </div>
        <normal-form-plus
          class="newAccountReport-content-form-wen"
          :formModel="formModels.formModel3"
          :config="configs.config3"
          :plus="plus1"/>
      </div>
    </div>

    <!-- 操作按钮 -->
    <div class="newAccountReport-btn">
      <el-button>返回</el-button>
      <el-button type="primary" @click="createEnterpriseReport()">保存</el-button>
    </div>

    <!-- 减排场景-选择 -->
    <el-dialog
      title="减排场景-选择"
      :visible.sync="showWindow"
      width="60%"
      class="form">
      <!-- form表单 -->
      <div class="formData">
        <normal-form
          ref="selectForm"
          :formModel="addFormModel"
          :config="addConfig"
          :plus="addPlus"/>
      </div>
      <span slot="footer" class="form-footer">
          <el-button type="primary" @click="makeSure()" class="confirm">确 定</el-button>
          <el-button @click="modifyWindow = false">取 消</el-button>
        </span>
    </el-dialog>
  </div>
</template>

<script>
import NormalFormPlus from '@/components/NormalFormPlus'
import NormalForm from '@/components/NormalForm'

export default {
  components: {
    NormalFormPlus,
    NormalForm
  },
  mounted() {
    this.getSelectItem();
  },
  data() {
    return {
      accountReportId: '',
      addPlus: {
        select: true,
        position: 'top',
        selectLabels: [],
        edit: false,
        tableLabel: [
          {
            edit: false,
            prop: "name",
            label: "滅排场景名称",
            width: 200
          },
          {
            edit: false,
            prop: "totalValue",
            label: "总减排量"
          },
          {
            edit: false,
            prop: "perValue",
            label: "单位减排量",
            width: 200
          },
        ]
      },
      addFormModel: {
        tableData: []
      },
      addConfig: [
        {
          id: "1",
          type: 'table',
          model: 'tableData',
          name: '减排场景',
          props: {
            placeholder: '减排场景',
          }
        }
      ],
      showWindow: false,
      plus1: {
        edit: false,
        tableLabel: [
          {
            prop: "name",
            label: "滅排场景名称",
            width: 200
          },
          {
            prop: "totalValue",
            label: "总减排量"
          },
          {
            prop: "perValue",
            label: "单位减排量",
            width: 200
          },
        ]
      },
      plus: {},
      formModels: {
        formModel1: {
          name: '', // 报告名称
          accountId: '', // 核算ID
          planId: '', // 控制计划ID
        },
        formModel2: {
          personInfo: '', // 	报告负责人信息
          personMobile: '', // 报告负责人电话
          personEmail: '' // 报告负责人邮箱
        },
        formModel3: {
          factor: []
        },
        formModel4: {},
      },
      configs: {
        config1: [
          {
            id: 1,
            items: [
              {
                id: '1',
                edit: true,
                type: 'input',
                span: 8,
                model: 'name',
                name: '报告名称',
                width: 40,
                props: {
                  placeholder: "报告名称",
                  width: 200
                }
              },
              {
                id: '2',
                edit: true,
                type: 'select',
                span: 8,
                model: 'accountId',
                name: '核算名称',
                width: 40,
                props: {
                  placeholder: "核算名称",
                  width: 200,
                  options: []
                }
              },
              {
                id: '3',
                edit: true,
                type: 'select',
                span: 8,
                model: 'planId',
                name: '数据质量控制计划版本',
                width: 150,
                props: {
                  placeholder: "数据质量控制计划版本",
                  options: []
                }
              },
            ]
          },
        ],
        config2: [
          {
            id: 1,
            items: [
              {
                id: '1',
                edit: true,
                type: 'input',
                span: 8,
                model: 'personInfo',
                name: '报告负责人姓名',
                width: 60,
                props: {
                  placeholder: "报告负责人姓名",
                  width: 200
                }
              },
              {
                id: '2',
                edit: true,
                type: 'input',
                span: 8,
                model: 'personMobile',
                name: '报告负责人电话',
                width: 60,
                props: {
                  placeholder: "报告负责人电话",
                  width: 200
                }
              },
              {
                id: '3',
                edit: true,
                type: 'input',
                span: 8,
                model: 'personEmail',
                name: '报告负责人邮箱',
                width: 60,
                props: {
                  placeholder: "报告负责人邮箱",
                  width: 200
                }
              },

            ]
          },
        ],
        config3: [
          {
            id: 1,
            items: [
              {
                id: '1',
                edit: true,
                type: 'table',
                span: 8,
                model: 'factor',
                name: '减排信息',
                width: 60,
                props: {
                  placeholder: "减排信息",

                }
              }
            ]
          }
        ],
      }
    }
  },
  methods: {
    // 创建核算报告
    createEnterpriseReport() {
      this.$API.enterprise.createEnterpriseReport({
        ...this.formModels.formModel1,
        ...this.formModels.formModel2,
        reductionAdvice: ''
      }).then(response => {
        // 获取创建核算报告的Id
        this.accountReportId = response.data;
        for (var i = 0; i < this.formModels.formModel3.factor.length; i++) {
          this.createEnterpriseReportAndReduction(this.formModels.formModel3.factor[i].id)
        }
        this.$router.push("/enterpriseCarbonAccounting/emission/report")
      })
    },
    // 创建报告和减排场景的链接
    createEnterpriseReportAndReduction(reductionId) {
      this.$API.enterprise.createEnterpriseReportAndReduction({
        reportId: this.accountReportId,
        reductionId: reductionId
      }).then(response => {
      })
    },
    // 获取accountId、planId列表
    getSelectItem() {
      this.configs.config1[0].items[1].options = []
      this.configs.config1[0].items[2].options = []
      // 获取核算列表
      this.$API.enterprise.getAccountingByPage({
        pageNo: 1,
        pageSize: 100,
      }).then(response => {
        var data = response.data.list
        var li = []
        for (var i = 0; i < data.length; i++) {
          li.push({
            label: data[i].name,
            value: data[i].id
          })
        }
        this.configs.config1[0].items[1].props.options = li
      })
      // 获取控制计划列表
      this.$API.enterprise.getControlPlanList({
        pageNo: 1,
        pageSize: 100,
        version: null
      }).then(response => {
        var data = response.data.list
        var li = []
        for (var i = 0; i < data.length; i++) {
          li.push({
            label: data[i].version,
            value: data[i].id
          })
        }
        this.configs.config1[0].items[2].props.options = li
      })

      // 获取减排场景
      this.$API.enterprise.getReductionSceneList({
        name: null,
        pageNo: 1,
        pageSize: 100,
      }).then(response => {
        // 处理数据格式
        var temData = response.data.list
        for (var i = 0; i < temData.length; i++) {
          temData[i].perValue = temData[i].perValue + temData[i].perUnit
          temData[i].totalValue = temData[i].totalValue + temData[i].totalUnit
        }
        this.addFormModel.tableData = temData
      })
    },
    turn() {
      this.showWindow = true
    },
    makeSure() {
      this.showWindow = false
      var li = this.$refs.selectForm.getSelectArray()
      this.formModels.formModel3.factor = li
    }
  }
}
</script>

<style lang="less" scoped>
.newAccountReport-content {

  padding: 10px;
  margin: 10px 0px;

  .newAccountReport-content-title {
    font-size: 18px;
    font-weight: 800;
  }

  .newAccountReport-content-form {
    margin: 20px 0px;

    .newAccountReport-content-form-wen {
      width: 230vw;
    }

    .newAccountReport-content-form-btn {

    }
  }
}
</style>
